package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.ResourceMapper;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;

    final ResourceMapper  resourceMapper;

    /**
     * 角色列表分页查询
     *
     * @param pageNum  当前页
     * @param pageSize 每页记录数
     * @param dto
     */
    @Override
    @Cacheable(cacheNames = "role",key = "#pageNum + #pageSize + #dto.hashCode()")      //查询添加缓存，优化下次查询速度
    public PageBean<RoleVo> rolePageQuery(Integer pageNum, Integer pageSize, RoleDto dto) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Role> roles = roleMapper.selectPage(dto);
        return  PageBean.of(roles, RoleVo.class);
    }

    /**
     * 根据角色ID查询选中的资源数据
     *
     * @param roleId
     * @return
     */
    @Override
    @Cacheable(cacheNames = "role",key = "#roleId")             //查询添加缓存，优化下次查询速度
    public List<String> findCheckedResources(Long roleId) {

        List<String> resourceIds = new ArrayList<>(roleResourceMapper.selectResourceNoByRoleId(roleId));

        return resourceIds;
    }

    /**
     * 添加角色
     *
     * @param roleDto
     */
    @Override
    @CacheEvict(cacheNames = "role", allEntries = true)         //增删改操作会修改数据库的值，但查询仍是缓存的值，此时就需要清缓存
    public void createRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.insert(role);
    }

    /**
     * 修改角色
     *
     * @param roleDto
     */
    @Override
    @Transactional
    @CacheEvict(cacheNames = "role", allEntries = true)         //增删改操作会修改数据库的值，但查询仍是缓存的值，此时就需要清缓存
    public void updateRole(RoleDto roleDto) {
//        1.解决启用禁用，编辑功能
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.updateByPrimaryKeySelective(role);

//        2.角色授权功能
        String[] checkedResourceNos = roleDto.getCheckedResourceNos();
        if(ArrayUtil.isEmpty(checkedResourceNos)){
            return;
        }

//        3.删中间表信息
        roleResourceMapper.deleteByRoleId(roleDto.getId());

//        4.增加中间表信息
        for (String resourceNo : checkedResourceNos) {
            RoleResource roleResource = RoleResource.builder()
                    .roleId(roleDto.getId())
                    .resourceNo(resourceNo)
                    .dataState("0")   //0表示启用
                    .build();
            roleResourceMapper.insert(roleResource);
        }
    }

    /**
     * 删除角色
     *
     * @param roleIds
     */
    @Override
    @CacheEvict(cacheNames = "role", allEntries = true)     //增删改操作会修改数据库的值，但查询仍是缓存的值，此时就需要清缓存
    public void deleteRole(String roleIds) {
//        1.先删角色
        Long roleId = Long.valueOf(roleIds);
        roleMapper.deleteByPrimaryKey(roleId);


//        2.删除子资源
        roleResourceMapper.deleteByRoleId(roleId);
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    @Cacheable(cacheNames = "role",key = "#root.methodName")        //查询添加缓存，优化下次查询速度
    public List<RoleVo> selectAllRoles() {
        List<Role> list = roleMapper.list();
        return BeanUtil.copyToList(list, RoleVo.class);
    }
}
